const app = getApp();

Page({
  setdata: app.setdata,
  goto: app.goto,
  input: app.input,
  /**
   * 页面的初始数据
   */
  data: {
    type:1,
    mrRoom:{},

    JSHJ:0,
    ORDER_ID:"",

    GMF_NSRSBH:"",
    GMF_MC:"",
    GMF_DZDH:"",
    GMF_YHZH:"",
    JSHJ:"",
    BZ:"",
    EMAIL:"",
    SMS:"",

    order_id:"",
    appid:"",

    cardauth:"",
    kpje:"",

    jflx: 0,

  },

  isWeChatCardAuth:function(e){
    console.log(e.detail.value);

    let cardauth = "";
    if (e.detail.value){
      cardauth = 1;
    }else{
      cardauth = 0;
    }
    this.setData({
      cardauth: cardauth
    })

    app.api.client_invoice_isWeChatCardAuth({
      token: wx.getStorageSync("token"),
      type: cardauth
    }, (res) => {

    })


  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    this.setData({
      type: options.type,
      JSHJ: options.JSHJ,
      ORDER_ID: options.ORDER_ID,
    })


    app.init(this);

    this.setData({
      mrRoom: wx.getStorageSync("mrRoom"),
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (this.data.order_id!=""){
      // app.toast(this.data.order_id);

      app.api.client_invoice_openInvoice({
        token: wx.getStorageSync("token"),
        ORDER_TYPE: this.data.type,
        IDROOM: this.data.mrRoom.idRoom,
        ORDER_ID: this.data.ORDER_ID,
        OPEN_INVOICE_TYPE: this.data.GMF_NSRSBH == "" ? 0 : 1,

        GMF_NSRSBH: this.data.GMF_NSRSBH,
        GMF_MC: this.data.GMF_MC,
        GMF_DZDH: this.data.GMF_DZDH,
        GMF_YHZH: this.data.GMF_YHZH,
        JSHJ: this.data.JSHJ,
        BZ: this.data.BZ,
        EMAIL: this.data.EMAIL,
        SMS: this.data.SMS,
        WXORDERID: this.data.order_id,
        WXAPPID: 'wxaf1f3969a71ae3b2',
        

        interface_error: "1",
      }, (res) => {
        this.setData({
          order_id: '',
          appid: '',
        })
        if (res.status == 1) {
          // wx.navigateTo({
          //   url: '../bill/history'
          // })
          wx.navigateBack({
            delta: 2
          })
        } else {
          app.toast(res.msg);
        }
        
      })
    }

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  client_invoice_openInvoice: function () {
    wx.showLoading({
      title: '...',
    });


    app.api.client_invoice_weChatCardAuth({
      token: wx.getStorageSync("token"),
      idCommunity: wx.getStorageSync("idCommunity"),
      kpje: this.data.JSHJ
    }, (res) => {
      console.log(res);
      this.setData({
        order_id: res.data.WXORDERID
      })
      wx.navigateToMiniProgram({
        appId: res.data.appid,
        path: res.data.auth_url,
        success(res) {
          wx.hideLoading();
          console.log('navigateToMiniProgram success:', res)
        },
        fail(error) {
          console.log('navigateToMiniProgram fail:', error)
        },
        complete(res) {
          console.log('navigateToMiniProgram complete:', res)
        }
      })
    })



  }


})